﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using _619BJP.Models;

namespace _619BJP.Database
{
  public  class productuploadDB
    {

      private string connString = System.Configuration.ConfigurationManager.ConnectionStrings["619BJP.Data.ConnectionString"].ConnectionString;

      public int uploadProduct(ProductModel productModel)
      {
          int ProductID;
          using (SqlConnection conn = new SqlConnection(connString))
          {
              string sql = "INSERT INTO [PR_Product]([brand_id],[product_name],[description],[img_url],[created_by],[created_time],[is_deleted]) VALUES(@brand_id,@product_name,@description,@img_url,@created_by,@created_time,0);Select @@IDENTITY";
              SqlParameter[] paramsToSql = new SqlParameter[6];
              paramsToSql[0] = new SqlParameter("@brand_id", SqlDbType.Int);
              paramsToSql[0].Value = 0;
              paramsToSql[1] = new SqlParameter("@product_name", SqlDbType.NVarChar);
              paramsToSql[1].Value = productModel.product_name;
              paramsToSql[2] = new SqlParameter("@description", SqlDbType.Text);
              paramsToSql[2].Value = productModel.description;
              paramsToSql[3] = new SqlParameter("@img_url", SqlDbType.NVarChar);
              paramsToSql[3].Value = productModel.img_url;
              paramsToSql[4] = new SqlParameter("@created_by", SqlDbType.NVarChar);
              paramsToSql[4].Value = "admin";
              paramsToSql[5] = new SqlParameter("@created_time", SqlDbType.DateTime);
              paramsToSql[5].Value = System.DateTime.Now;

              ProductID = Convert.ToInt32(SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, paramsToSql));

              sql = "INSERT INTO [PR_ProductInfo]([product_id],[category_id],[producer],[approve_number],[product_specification],[health_functions],[suit_crowds],[market_price],[created_by],[created_time],[is_deleted]) VALUES(@product_id,@category_id,@producer,@approve_number,@product_specification,@health_functions,@suit_crowds,@market_price,@created_by,@created_time,0)";
              SqlParameter[] paramsToSql2 = new SqlParameter[10];
              paramsToSql2[0] = new SqlParameter("@product_id", SqlDbType.Int);
              paramsToSql2[0].Value = ProductID;
              paramsToSql2[1] = new SqlParameter("@category_id", SqlDbType.Int);
              paramsToSql2[1].Value = productModel.category_id;
              paramsToSql2[2] = new SqlParameter("@producer", SqlDbType.NVarChar);
              paramsToSql2[2].Value = productModel.producer;
              paramsToSql2[3] = new SqlParameter("@approve_number", SqlDbType.NVarChar);
              paramsToSql2[3].Value = productModel.approve_number;
              paramsToSql2[4] = new SqlParameter("@product_specification", SqlDbType.NVarChar);
              paramsToSql2[4].Value = productModel.product_specification;
              paramsToSql2[5] = new SqlParameter("@health_functions", SqlDbType.NVarChar);
              paramsToSql2[5].Value = productModel.health_functions;
              paramsToSql2[6] = new SqlParameter("@suit_crowds", SqlDbType.NVarChar);
              paramsToSql2[6].Value = productModel.suit_crowds;
              paramsToSql2[7] = new SqlParameter("@market_price", SqlDbType.Float);
              paramsToSql2[7].Value = productModel.market_price;
              paramsToSql2[8] = new SqlParameter("@created_by", SqlDbType.NVarChar);
              paramsToSql2[8].Value = "admin";
              paramsToSql2[9] = new SqlParameter("@created_time", SqlDbType.DateTime);
              paramsToSql2[9].Value = System.DateTime.Now;

              SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, paramsToSql2);
           
          }






          return ProductID;
      
      }


    }
}
